@extends('admin.layout')

@section('content')
    <div class="layui-fluid">
        <div class="layui-card">
            <div class="layui-card-header layuiadmin-card-header-auto">
                @can('content.block.create')
                <button class="layui-btn layuiadmin-btn-com" data-type="add">添加</button>
                @endcan
            </div>
            <div class="layui-card-body">
                <div class="layui-tab layui-tab-card">
                    <ul class="layui-tab-title">
                        @foreach($block_types as $block_type)
                        <li @if($block_type->id==1)class="layui-this"@endif>{{ $block_type->name }}</li>
                        @endforeach
                    </ul>
                    <div class="layui-tab-content">
                        <div class="layui-tab-item layui-show">
                            <table id="LAY-app-content-block" lay-filter="LAY-app-content-block"></table>
                            <script type="text/html" id="options">
                                <div class="layui-btn-group">
                                    @can('content.block.edit')
                                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit"><i class="layui-icon layui-icon-edit"></i>编辑</a>
                                    @endcan
                                    @can('content.block.destroy')
                                         <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del"><i class="layui-icon layui-icon-delete"></i>删除</a>
                                    @endcan
                                </div>
                            </script>
                            <script type="text/html" id="position">
                                @{{ d.block_type.name }}
                            </script>
                            <script type="text/html" id="thumb">
                                <a href="@{{d.image}}" target="_blank" title="点击查看"><img src="@{{d.image}}" alt="" width="28" height="28"></a>
                            </script>
                        </div>
                        <div class="layui-tab-item">2</div>
                        <div class="layui-tab-item">3</div>
                    </div>
                </div>

        </div>
    </div>
@endsection

@section('js')
    @can('content.block')
        <script>
            layui.config({
                base: '/static/layuiadmin/' //静态资源所在路径
            }).extend({
                index: 'lib/index' //主入口模块
            }).use(['index', 'contlist','table'],function(){
                var $ = layui.$, table = layui.table, form = layui.form;

                //监听搜索
                form.on('submit(LAY-app-contlist-search)', function(data){
                    const field = data.field;

                    //执行重载
                    table.reload('LAY-app-content-block', {
                        where: field
                    });
                });

                const active = {
                    batchdel: function(){
                        var checkStatus = table.checkStatus('LAY-app-content-list')
                            ,checkData = checkStatus.data; //得到选中的数据

                        if(checkData.length === 0){
                            return layer.msg('请选择数据');
                        }

                        layer.confirm('确定删除吗？', function(index) {

                            //执行 Ajax 后重载
                            $.post("{{ route('admin.block.destroy') }}",{_method:'delete',ids:ids},function (result) {
                                if (result.code===0){
                                    dataTable.reload()
                                }
                                layer.close(index);
                                layer.msg(result.msg,{icon:6})
                            });
                            table.reload('LAY-app-content-list');
                            layer.msg('已删除');
                        });
                    },
                    add: function () {
                        layer.open({
                            type: 2
                            , title: '添加类型'
                            , content: 'block/create'
                            , maxmin: true
                            , area: ['450px', '300px']
                            , btn: ['确定', '取消']
                            , yes: function (index, layero) {
                                //点击确认触发 iframe 内容中的按钮提交
                                var submit = layero.find('iframe').contents().find("#layuiadmin-app-form-submit");
                                submit.click();
                            }
                        });
                    }
                };
                $('.layui-btn.layuiadmin-btn-com').on('click', function(){
                    var type = $(this).data('type');
                    active[type] ? active[type].call(this) : '';
                });
            })
        </script>
    @endcan
@endsection
